<?php

if(!in_array("sfYaml", get_declared_classes())){
  @include "/home/excch/lib/symfony/sfYaml/sfYaml.php";
}

class ytClock
{
	var $uhr = array();
	var $class_on = "on";
	var $class_off = "off";
	
	public function set_layout($layout){
		$clockYaml = new sfYaml;
		$this->uhr = $clockYaml->load($layout);
	}
	
	private function css_class(&$var){
		$ret = ($var['stat'] == 0) ? $this->class_off : $this->class_on;
		return $ret;
	}
	
	private function turn_on($array){
		foreach($array as $letter_on){
			$coord = explode('-',$letter_on);
			$row_on = $coord[0];
			$cell_on = $coord[1];
			$this->uhr['grid'][$row_on][$cell_on]['stat'] = 1;
		}	
	}
	
	private function set_time($s_time){
		if (isset($s_time)){
			$hour = substr($s_time, 0, 2)-1;
			$hour = ($hour > 11) ? $hour-12 : $hour;
			$min = substr($s_time, 2, 2);
		}
		else{
			$hour = date('g', time())-1;
			$min = date('i', time());
		}
		
		// -- dots -- //
		$add_min = $min%5;
		switch ($add_min){
			case 4:
				$this->uhr['dot'][1][0]['stat'] = 1;
			case 3:
				$this->uhr['dot'][1][1]['stat'] = 1;
			case 2:
				$this->uhr['dot'][0][1]['stat'] = 1;
			case 1:
				$this->uhr['dot'][0][0]['stat'] = 1;
				break;
		}
		
		// -- minutes -- //
		$five_min = floor($min/5);
		if(is_array($this->uhr['min'][$five_min]['word'])){
			foreach($this->uhr['min'][$five_min]['word'] as $m_word=>$m_key){
				$this->turn_on($this->uhr['word'][$m_key]);
			}
		}
		
		// -- hours -- //
		$hour += (int)$this->uhr['min'][$five_min]['inc_h'];
		$hour = ($hour > 11) ? $hour-12 : $hour;
		$this->turn_on($this->uhr['hour'][$hour]);
		
		// -- handle additional letters for hournames -- //
		$add_l = $hour+1;
		if($five_min != 0 && isset($this->uhr['add'][$add_l])){
			$this->turn_on($this->uhr['add'][$add_l]);
		}
		
	}
	
	public function create($c_time=NULL){
	
		$this->set_time($c_time);
		
		$t_uhr = $this->uhr;
		echo "<table id=\"clock\">\n<tr>\n";
		echo "<td class=\"".$this->css_class($t_uhr['dot'][0][0])."\">&middot;</td>\n";
		echo "<td colspan=\"11\"></td>\n";
		echo "<td class=\"".$this->css_class($t_uhr['dot'][0][1])."\">&middot;</td>\n";
		echo "</tr>";
		foreach($t_uhr['grid'] as $row){
			echo "<tr>\n<td></td>\n";
			foreach($row as $cell){
				echo "<td class=\"".$this->css_class($cell)."\">{$cell['char']}</td>\n";
			}
			echo "<td></td>\n</tr>\n";
		}
		echo "<tr>\n";
		echo "<td class=\"".$this->css_class($t_uhr['dot'][1][0])."\">&middot;</td>\n";
		echo "<td colspan=\"11\"></td>\n";
		echo "<td class=\"".$this->css_class($t_uhr['dot'][1][1])."\">&middot;</td>\n";
		echo "</tr>";
		echo "</table>\n";
	}
}

?>
